<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/15
 * Time: 17:19
 */

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class ShopMemberCart extends Model {
	/**
	 * 用户购物车
	 * @var string
	 */
	protected $table = 'shop_member_cart';

	protected $fillable = [
		'id',
		'uid',
		'openid',
		'goodsid',
		'goodsname',
		'number',
		'rulingprice',
		'specs',
		'createtime',
	];

	/**
	 * 统计购物车
	 * @param null $uid
	 * @return int
	 */
	public function count_cart($uid = null) {
		return $this->where(['uid' => $uid])->count();
	}

	/**
	 * 添加购物车
	 * @param array $data
	 * @return $this|Model
	 */
	public function add_cart($data = array()) {
		return $this->create($data);
	}

	/**
	 * 查询购物车
	 * @param null $id 购物车id
	 * @param null $goods_id 商品id
	 * @param null $uid 会员id
	 * @return bool|Model|null|static
	 */
	public function find_cart($uid = null, $goods_id = null, $id = null) {
		$table = $this->where(['uid' => $uid]);
		if ($id) {
			$table->where(['id' => $id]);
		}
		if ($goods_id) {
			$table->where(['goodsid' => $goods_id]);
		}
		$rico = $table->first();
		if ($rico) {
			return $rico;
		} else {
			return false;
		}
	}

	/**
	 * 查询购物车
	 * @param null $uid
	 * @param null $goods_id
	 * @return array|bool
	 */
	public function get_cart($uid = null, $goods_id = null) {
		$table = $this->where(['uid' => $uid]);
		if ($goods_id) {
			$table->where(['goodsid' => $goods_id]);
		}
		$rico = $table->get()->toArray();
		if ($rico) {
			return $rico;
		} else {
			return false;
		}
	}
	/**
	 * 删除购物车
	 * @param null $ids
	 * @return bool|null
	 * @throws \Exception
	 */
	public function delete_cart($ids = null) {
		$rico = $this->destroy($ids);
		if ($rico) {
			return $rico;
		} else {
			return false;
		}
	}
}